from Tkinter import *

class Line:
	def __init__(self,x=200, y=50, x1=300, y1=50):
		self.x = x
		self.y = y
		self.x1 = x1
		self.y1 = y1
		self.line = canv.create_line([x,y],[x1,y1],width=2)
	def isbelong(self, flag):
		if (flag):
			self.arc1 = canv.create_oval([self.x-3,self.y+3],[self.x+3,self.y-3],fill="black")
			self.arc2 = canv.create_oval([self.x1-3,self.y1+3],[self.x1+3,self.y1-3],fill="black")
			return True
		else:
			try:
				canv.delete(self.arc1)
				canv.delete(self.arc2)
				return False
			except:
				return True
				
	def isbelongs(self, x, y):
		if (y <= ((x-self.x)*(self.y1-self.y))/(self.x1-self.x)+self.y+3 and y >= ((x-self.x)*(self.y1-self.y))/(self.x1-self.x)+self.y-3):
			self.prevx = x
			self.prevy = y
			canv.bind('<B1-Motion>',self.moveall)
			canv.bind('<ButtonRelease-1>',self.endmove)
			return True
		return False
	def moveall(self, event):
		canv.delete(self.line)
		canv.delete(self.arc1)
		canv.delete(self.arc2)
		self.line = canv.create_line([self.x,self.y],[self.x1,self.y1],width=2)
		self.arc1 = canv.create_oval([self.x-3,self.y+3],[self.x+3,self.y-3],fill="black")
		self.arc2 = canv.create_oval([self.x1-3,self.y1+3],[self.x1+3,self.y1-3],fill="black")
		self.x = event.x-self.prevx+self.x
		self.y = event.y-self.prevy+self.y
		self.x1 = event.x-self.prevx+self.x1
		self.y1 = event.y-self.prevy+self.y1
		self.prevx = event.x
		self.prevy = event.y
	def isarc(self, x, y):
		if ((x - self.x)*(x - self.x) + (y - self.y)*(y - self.y) <= 3*3):
			canv.bind('<B1-Motion>',self.movearc1)
			canv.bind('<ButtonRelease-1>',self.endmove)
			return True
		elif ((x - self.x1)*(x - self.x1) + (y - self.y1)*(y - self.y1) <= 3*3):
			canv.bind('<B1-Motion>',self.movearc2)
			canv.bind('<ButtonRelease-1>',self.endmove)
			return True
	def movearc1(self, event):
		self.x = event.x
		self.y = event.y
		canv.delete(self.line)
		canv.delete(self.arc1)
		self.line = canv.create_line([self.x,self.y],[self.x1,self.y1],width=2)
		self.arc1 = canv.create_oval([self.x-3,self.y+3],[self.x+3,self.y-3],fill="black")
	def movearc2(self, event):
		self.x1 = event.x
		self.y1 = event.y
		canv.delete(self.line)
		canv.delete(self.arc2)
		self.line = canv.create_line([self.x,self.y],[self.x1,self.y1],width=2)
		self.arc2 = canv.create_oval([self.x1-3,self.y1+3],[self.x1+3,self.y1-3],fill="black")
	def endmove(self, event):
		canv.unbind('<B1-Motion>')
		canv.unbind('<ButtonRelease-1>')
	def __del__(self):
		try:
			canv.delete(self.line)
			canv.delete(self.arc1)
			canv.delete(self.arc2)
		except:
			pass

class But_create_line:
	def __init__(self):
		self.but = Button(root, text="Create")
		self.but.bind("<Button-1>",self.create_line)
		self.but.pack()
	def create_line(self,event):
		v1 = value1.get()
		v2 = value2.get()
		v3 = value3.get()
		v4 = value4.get()
		lines.append(Line(v1,v2,v3,v4))

class But_del_line:
	def __init__(self):
		self.but = Button(root, text="Delete")
		self.but.bind("<Button-1>",self.del_line)
		self.but.pack()
	def del_line(self,event):
		if (selected >= 0):
			lines[selected].__del__()
			del lines[selected]
def click(event):
	global selected
	for line in lines:
		if (selected == lines.index(line) and line.isarc(event.x, event.y)):
			selected = lines.index(line)
		elif (line.isbelongs(event.x, event.y)):
			if (selected != lines.index(line)):
				line.isbelong(True)
				selected = lines.index(line)
				break
		else:
			if (line.isbelong(False)):
				selected = -1

selected = -1
root=Tk()
lines = list()
canv=Canvas(root,width=600,height=400,bg='#FFFFFF')
value1 = DoubleVar()
value2 = DoubleVar()
value3 = DoubleVar()
value4 = DoubleVar()
value1.set(200)
value2.set(50)
value3.set(300)
value4.set(50)
ent1 = Entry (root, textvariable = value1)
ent2 = Entry(root, textvariable = value2)
ent3 = Entry (root, textvariable = value3)
ent4 = Entry(root, textvariable = value4)
ent1.pack()
ent2.pack()
ent3.pack()
ent4.pack()
butcreate = But_create_line()
butdel = But_del_line()
canv.bind('<Button-1>',click)
canv.pack()
root.mainloop()
